package it.low.game.client.gui;

import it.low.game.client.gui.button.Button;
import it.low.game.client.gui.command.CommandClientExit;
import it.low.game.client.gui.command.CommandMenuClose;
import it.low.game.client.gui.command.CommandMenuOpen;
import it.low.game.client.gui.input.Input.EXEC;
import it.low.game.client.gui.input.InputKeyButton;
import it.low.game.client.gui.input.InputManager;
import it.low.game.client.gui.panel.GamePanel;
import it.low.game.client.gui.panel.MenuPanel;
import it.low.game.client.util.ClientLogManager;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Container;
import java.awt.DisplayMode;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Point;
import java.awt.event.KeyEvent;

import javax.swing.JComponent;
import javax.swing.JFrame;

public class ClientScreen extends JFrame {
	
	private DisplayMode displayMode = null;
	private ScreenManager screenManager = null;
	private InputManager inputManager = null;
	
	private MenuPanel menuPanel = null;
	public GamePanel gamePanel = null;

	public ClientScreen(int width, int height, int bitDepth){
		this.displayMode = new DisplayMode(width,height,bitDepth,DisplayMode.REFRESH_RATE_UNKNOWN);
		this.setBackground(Color.DARK_GRAY);
		this.setFont(new Font("Dialog", Font.PLAIN, 24));
		this.screenManager = new ScreenManager();
	}
		
	public void init(Point gamePanelLocation){
		ClientLogManager.logMsg("call GameScreen.init()");
				
		// INIT THE SCREEN MANAGER		
		this.screenManager.setFullScreen(displayMode, this);
		
		// INIT THE INPUT MANAGER
		inputManager = new InputManager(screenManager.getFullScreenWindow());
		inputManager.init();
		
		// INIT THE SCREEN
		JFrame frame = this.screenManager.getFullScreenWindow();
		Container container = frame.getContentPane();
		
		if (container instanceof JComponent) {
			((JComponent)container).setOpaque(false);
		}
		
		container.setLayout(new BorderLayout());
		
		this.initMenuPanel();	
		this.initGamePanel(gamePanelLocation);				
	}
	
	public void close(){
		ClientLogManager.logMsg("call GameScreen.close()");
		screenManager.restoreScreen();
	}
	
	public void paint(Graphics g){		
		// MISSING ANTI-ALIASING (pag. 52)
		JFrame frame = this.screenManager.getFullScreenWindow();
		frame.getLayeredPane().paintComponents(g);
	}
		
	
	private void initMenuPanel(){
		ClientLogManager.logMsg("call GameScreen.initMenuPanel()");
		
		// CREATE MENU PANEL		
		menuPanel = new MenuPanel();
		
		// CREATE BUTTONS
		CommandClientExit commandClientExit = new CommandClientExit();
		InputKeyButton inputClientExit = new InputKeyButton("Exit", KeyEvent.VK_ESCAPE, EXEC.SELECT, commandClientExit);
		Button exitButton = new Button(inputClientExit);
		
		CommandMenuOpen commandMenuOpen = new CommandMenuOpen();
		CommandMenuClose commandMenuClose = new CommandMenuClose();
		InputKeyButton inputMenu = new InputKeyButton("Menu", KeyEvent.VK_F1, EXEC.TOGGLE, commandMenuOpen,commandMenuClose);
		Button menuButton = new Button(inputMenu);
				
		// ADD BUTTONS
		menuPanel.add(exitButton);
		menuPanel.add(menuButton);
		
		// ADD BUTTONS MANAGER
		inputManager.addButton(exitButton);
		inputManager.addButton(menuButton);
		
		// ADD MENU PANEL
		this.getContentPane().add(menuPanel,BorderLayout.PAGE_START);
	}
	
	private void initGamePanel(Point gamePanelLocation){
		ClientLogManager.logMsg("call GameScreen.initGamePanel()");
		
		// CREATE GAME PANEL
		gamePanel = new GamePanel(gamePanelLocation);
						
		// ADD INPUT MANAGER
		inputManager.addBoard(gamePanel);
		
		// ADD GAME PANEL
		this.getContentPane().add(gamePanel,BorderLayout.CENTER);		
	}	

}
